home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / library / reqtlsdv.lha / ReqTools / demo / demo.c < prev    next >
C/C++ Source or Header  |  1996-04-21  |  14KB  |  448 lines

  1. /*********************************
  2. *                                *
  3. *  reqtools.library (V38)        *
  4. *                                *
  5. *  Release 2.5                   *
  6. *                                *
  7. *  (c) 1991-1994 Nico François   *
  8. *      1995-1996 Nico François   *
  9. *                                *
  10. *  demo.c                        *
  11. *                                *
  12. *  This source is public domain  *
  13. *  in all respects.              *
  14. *                                *
  15. *********************************/
  16.  
  17. #include <exec/types.h>
  18. #include <exec/memory.h>
  19. #include <exec/execbase.h>
  20. #include <libraries/dos.h>
  21. #include <intuition/intuition.h>
  22. #include <proto/exec.h>
  23. #include <proto/dos.h>
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. #include <libraries/reqtools.h>
  30. #include <proto/reqtools.h>
  31.  
  32. extern struct ExecBase *SysBase;
  33. struct ReqToolsBase *ReqToolsBase;
  34.  
  35. #define REG register
  36.  
  37. /* Callback functions
  38.     Define DOHOOKS when you compile to activate these filter functions */
  39.  
  40. BOOL __asm __saveds file_filterfunc (
  41.     REG __a0 struct Hook *, REG __a2 struct rtFileRequester *,
  42.     REG __a1 struct FileInfoBlock *
  43.     );
  44. BOOL __asm __saveds font_filterfunc (
  45.     REG __a0 struct Hook *, REG __a2 struct rtFontRequester *,
  46.     REG __a1 struct TextAttr *
  47.     );
  48. BOOL __asm __saveds vol_filterfunc (
  49.     REG __a0 struct Hook *, REG __a2 struct rtFileRequester *,
  50.     REG __a1 struct rtVolumeEntry *
  51.     );
  52.  
  53. ULONG undertag[] = { RT_Underscore, '_', TAG_END };
  54.  
  55. void myputs (char *str)
  56. {
  57.     Write (Output(), str, strlen(str));
  58. }
  59.  
  60. void __stdargs __main (char *line)
  61. {
  62.     struct rtFileRequester *filereq;
  63.     struct rtFontRequester *fontreq;
  64.     struct rtScreenModeRequester *scrmodereq;
  65.     struct rtFileList *flist, *tempflist;
  66.     struct Hook filterhook, font_filterhook, vol_filterhook;
  67.     char buffer[128], filename[34];
  68.     ULONG longnum, ret, color;
  69.  
  70.     if (!(ReqToolsBase = (struct ReqToolsBase *)
  71.                                           OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))) {
  72.         myputs ("You need reqtools.library V38 or higher!\n"
  73.                   "Please install it in your Libs: drirectory.\n");
  74.         exit (0);
  75.         }
  76.  
  77.     rtEZRequest ("ReqTools 2.0 Demo\n"
  78.                  "~~~~~~~~~~~~~~~~~\n"
  79.                      "'reqtools.library' offers several\n"
  80.                      "different types of requesters:",
  81.                      "Let's see them", NULL, NULL);
  82.  
  83.     rtEZRequest ("NUMBER 1:\nThe larch :-)", "Be serious!", NULL, NULL);
  84.  
  85.     rtEZRequest ("NUMBER 1:\nString requester\nfunction: rtGetString()",
  86.                      "Show me", NULL, NULL);
  87.  
  88.     strcpy (buffer, "A bit of text");
  89.     if (!rtGetString (buffer, 127, "Enter anything:", NULL, TAG_END))
  90.         rtEZRequest ("You entered nothing :-(", "I'm sorry", NULL, NULL);
  91.     else
  92.         rtEZRequest ("You entered this string:\n'%s'.",
  93.                          "So I did", NULL, NULL, buffer);
  94.  
  95.     ret = rtGetString (buffer, 127, "Enter anything:", NULL,
  96.                              RTGS_GadFmt, " _Ok |New _2.0 feature!|_Cancel",
  97.                              RTGS_TextFmt,
  98.                                  "These are two new features of ReqTools 2.0:\n"
  99.                                   "Text above the entry gadget and more than\n"
  100.                                   "one response gadget.",
  101.                      TAG_MORE, undertag);
  102.     if (ret == 2)
  103.         rtEZRequest ("Yep, this is a new\nReqTools 2.0 feature!",
  104.                          "Oh boy!", NULL, NULL);
  105.  
  106.     ret = rtGetString (buffer, 127, "Enter anything:", NULL,
  107.                              RTGS_GadFmt, " _Ok |_Abort|_Cancel",
  108.                              RTGS_TextFmt,
  109.                                  "New is also the ability to switch off the\n"
  110.                                   "backfill pattern.  You can also center the\n"
  111.                                   "text above the entry gadget.\n"
  112.                                   "These new features are also available in\n"
  113.                                   "the rtGetLong() requester.",
  114.                              RTGS_BackFill, FALSE,
  115.                              RTGS_Flags, GSREQF_CENTERTEXT|GSREQF_HIGHLIGHTTEXT,
  116.                              TAG_MORE, undertag);
  117.     if (ret == 2)
  118.         rtEZRequest ("What!! You pressed abort!?!\nYou must be joking :-)",
  119.                          "Ok, Continue", NULL, NULL);
  120.  
  121.  
  122.     rtEZRequest ("NUMBER 2:\nNumber requester\nfunction: rtGetLong()",
  123.                      "Show me", NULL, NULL);
  124.  
  125.     if (!rtGetLong (&longnum, "Enter a number:", NULL,
  126.                          RTGL_ShowDefault, FALSE,
  127.                          RTGL_Min, 0, RTGL_Max, 666,TAG_END))
  128.         rtEZRequest ("You entered nothing :-(",
  129.                          "I'm sorry", NULL, NULL);
  130.     else
  131.         rtEZRequest ("The number you entered was:\n%ld%s",
  132.                          "So it was", NULL, NULL, longnum,
  133.                             (longnum == 666) ? " (you devil! :)" : "");
  134.  
  135.     rtEZRequest ("NUMBER 3:\nMessage requester, the requester\n"
  136.                      "you've been using all the time!\nfunction: rtEZRequest()",
  137.                      "Show me more", NULL, NULL);
  138.  
  139.     rtEZRequest ("Simplest usage: some body text and\na single centered gadget.",
  140.                      "Got it", NULL, NULL);
  141.  
  142.     while (!rtEZRequest ("You can also use two gadgets to\n"
  143.                                 "ask the user something.\n"
  144.                                 "Do you understand?", "Of course|Not really",
  145.                                 NULL, NULL))
  146.         rtEZRequest ("You are not one of the brightest are you?\n"
  147.                          "We'll try again...",
  148.                          "Ok", NULL, NULL);
  149.  
  150.     rtEZRequest ("Great, we'll continue then.", "Fine", NULL, NULL);
  151.  
  152.     switch (rtEZRequest ("You can also put up a requester with\n"
  153.                                 "three choices.\n"
  154.                                 "How do you like the demo so far ?",
  155.                                 "Great|So so|Rubbish", NULL, NULL)) {
  156.         case FALSE:
  157.             rtEZRequest ("Too bad, I really hoped you\nwould like it better.",
  158.                              "So what", NULL, NULL);
  159.             break;
  160.         case TRUE:
  161.             rtEZRequest ("I'm glad you like it so much.", "Fine", NULL, NULL);
  162.             break;
  163.         case 2:
  164.             rtEZRequest ("Maybe if you run the demo again\n"
  165.                              "you'll REALLY like it.",
  166.                              "Perhaps", NULL, NULL);
  167.             break;
  168.         }
  169.  
  170.     ret = rtEZRequestTags ("The number of responses is not limited to three\n"
  171.                                   "as you can see.  The gadgets are labeled with\n"
  172.                                   "the return code from rtEZRequest().\n"
  173.                                   "Pressing Return will choose 4, note that\n"
  174.                                   "4's button text is printed in boldface.",
  175.                                   "1|2|3|4|5|0", NULL, NULL,
  176.                                   RTEZ_DefaultResponse, 4, TAG_END);
  177.     rtEZRequest ("You picked '%ld'.", "How true", NULL, NULL, ret);
  178.  
  179.     rtEZRequestTags ("New for Release 2.0 of ReqTools (V38) is\n"
  180.                           "the possibility to define characters in the\n"
  181.                           "buttons as keyboard shortcuts.\n"
  182.                           "As you can see these characters are underlined.\n"
  183.                           "Pressing shift while still holding down the key\n"
  184.                           "will cancel the shortcut.\n"
  185.                           "Note that in other requesters a string gadget may\n"
  186.                           "be active.  To use the keyboard shortcuts there\n"
  187.                           "you have to keep the Right Amiga key pressed down.",
  188.                           "_Great|_Fantastic|_Swell|Oh _Boy",
  189.                           NULL, NULL,
  190.                           RT_Underscore, '_', TAG_END);
  191.  
  192.     rtEZRequest (
  193.         "You may also use C-style formatting codes in the body text.\n"
  194.         "Like this:\n\n"
  195.         "'The number %%ld is written %%s.' will give:\n\n"
  196.         "The number %ld is written %s.\n\n"
  197.         "if you also pass '5' and '\"five\"' to rtEZRequest().",
  198.         "_Proceed", NULL, (struct TagItem *)&undertag, 5, "five");
  199.  
  200.     if (rtEZRequestTags ("It is also possible to pass extra IDCMP flags\n"
  201.                                "that will satisfy rtEZRequest(). This requester\n"
  202.                                "has had DISKINSERTED passed to it.\n"
  203.                                "(Try inserting a disk).",
  204.                                "_Continue", NULL, NULL,
  205.                                RT_IDCMPFlags, DISKINSERTED,
  206.                                 RT_Underscore, '_', TAG_END)
  207.                                & DISKINSERTED)
  208.         rtEZRequest ("You inserted a disk.", "I did", NULL, NULL);
  209.     else
  210.         rtEZRequest ("You used the 'Continue' gadget\n"
  211.                          "to satisfy the requester.", "I did", NULL, NULL);
  212.  
  213.     rtEZRequestTags ("Finally, it is possible to specify the position\n"
  214.                           "of the requester.\n"
  215.                           "E.g. at the top left of the screen, like this.\n"
  216.                           "This works for all requesters, not just rtEZRequest()!",
  217.                           "_Amazing", NULL, NULL,
  218.                           RT_ReqPos, REQPOS_TOPLEFTSCR,
  219.                           RT_Underscore, '_', TAG_END);
  220.  
  221.     rtEZRequestTags ("Alternatively, you can center the\n"
  222.                           "requester on the screen.\n"
  223.                           "Check out 'reqtools.doc' for all the possibilities.",
  224.                           "I'll do that", NULL, NULL,
  225.                           RT_ReqPos, REQPOS_CENTERSCR, TAG_END);
  226.  
  227.     rtEZRequestTags ("NUMBER 4:\nFile requester\n"
  228.                           "function: rtFileRequest()",
  229.                           "_Demonstrate", NULL, NULL, RT_Underscore, '_', TAG_END);
  230.  
  231.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  232.  
  233.         filterhook.h_Entry = (ULONG (*)())file_filterfunc;
  234.  
  235.         filename[0] = 0;
  236.         if (rtFileRequest (filereq, filename, "Pick a file",
  237. #ifdef DOHOOKS
  238.                                  RTFI_FilterFunc, &filterhook,
  239. #endif
  240.                                  TAG_END))
  241.             rtEZRequest ("You picked the file:\n'%s'\nin directory:\n'%s'",
  242.                              "Right", NULL, NULL, filename, filereq->Dir);
  243.         else
  244.             rtEZRequest ("You didn't pick a file.", "No", NULL, NULL);
  245.  
  246.         rtEZRequestTags ("The file requester has the ability\n"
  247.                               "to allow you to pick more than one\n"
  248.                               "file (use Shift to extend-select).\n"
  249.                               "Note the extra gadgets you get.",
  250.                               "_Interesting", NULL, NULL, RT_Underscore, '_', TAG_END);
  251.  
  252.         flist = rtFileRequest (filereq, filename, "Pick some files",
  253.                                       RTFI_Flags, FREQF_MULTISELECT, TAG_END);
  254.         if (flist) {
  255.             rtEZRequest ("You selected some files, this is\n"
  256.                              "the first one:\n"
  257.                              "'%s'\n"
  258.                              "All the files are returned as a linked\n"
  259.                              "list (see demo.c and reqtools.h).",
  260.                              "Aha", NULL, NULL, flist->Name);
  261.             /* Traverse all selected files */
  262.             /*
  263.             tempflist = flist;
  264.             while (tempflist) {
  265.                 DoSomething (tempflist->Name, tempflist->StrLen);
  266.                 tempflist = tempflist->Next;
  267.                 }
  268.             */
  269.             /* Free filelist when no longer needed! */
  270.             rtFreeFileList (flist);
  271.             }
  272.  
  273.         rtFreeRequest (filereq);
  274.         }
  275.     else
  276.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  277.  
  278.  
  279.     rtEZRequestTags ("The file requester can be used\n"
  280.                           "as a directory requester as well.",
  281.                           "Let's _see that", NULL, NULL, RT_Underscore, '_', TAG_END);
  282.  
  283.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  284.         if (rtFileRequest (filereq, filename, "Pick a directory",
  285.                                  RTFI_Flags, FREQF_NOFILES, TAG_END))
  286.             rtEZRequest ("You picked the directory:\n'%s'",
  287.                              "Right", NULL, NULL, filereq->Dir);
  288.         else
  289.             rtEZRequest ("You didn't pick a directory.", "No", NULL, NULL);
  290.  
  291.         rtFreeRequest (filereq);
  292.         }
  293.     else
  294.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  295.  
  296.     rtEZRequest ("NUMBER 5:\nFont requester\n"
  297.                      "function: rtFontRequest()",
  298.                      "Show", NULL, NULL);
  299.  
  300.     if (fontreq = rtAllocRequestA (RT_FONTREQ, NULL)) {
  301.  
  302.         fontreq->Flags = FREQF_STYLE|FREQF_COLORFONTS;
  303.         font_filterhook.h_Entry = (ULONG (*)())font_filterfunc;
  304.  
  305.         if (rtFontRequest (fontreq, "Pick a font",
  306. #ifdef DOHOOKS
  307.                                  RTFO_FilterFunc, &font_filterhook,
  308. #endif
  309.                                  TAG_END))
  310.             rtEZRequest ("You picked the font:\n'%s'\nwith size:\n'%ld'",
  311.                              "Right", NULL, NULL,
  312.                              fontreq->Attr.ta_Name, fontreq->Attr.ta_YSize);
  313.         else
  314.             rtEZRequestTags ("You canceled.\nWas there no font you liked ?",
  315.                                   "_Nope", NULL, NULL, RT_Underscore, '_', TAG_END);
  316.  
  317.         rtFreeRequest (fontreq);
  318.         }
  319.     else
  320.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  321.  
  322.     rtEZRequestTags ("NUMBER 6:\nPalette requester\n"
  323.                           "function: rtPaletteRequest()",
  324.                           "_Proceed", NULL, NULL, RT_Underscore, '_', TAG_END);
  325.  
  326.     color = rtPaletteRequest ("Change palette", NULL, TAG_END);
  327.     if (color == -1)
  328.         rtEZRequest ("You canceled.\nNo nice colors to be picked ?",
  329.                          "Nah", NULL, NULL);
  330.     else
  331.         rtEZRequest ("You picked color number %ld.", "Sure did",
  332.                          NULL, NULL, color);
  333.  
  334.     rtEZRequestTags ("NUMBER 7: (ReqTools 2.0)\n"
  335.                           "Volume requester\n"
  336.                           "function: rtFileRequest() with\n"
  337.                           "          RTFI_VolumeRequest tag.",
  338.                           "_Show me", NULL, NULL, RT_Underscore, '_', TAG_END);
  339.  
  340.     if (filereq = rtAllocRequestA (RT_FILEREQ, NULL)) {
  341.  
  342.         vol_filterhook.h_Entry = (ULONG (*)())vol_filterfunc;
  343.  
  344.         if (rtFileRequest (filereq, NULL, "Pick a volume",
  345. #ifdef DOHOOKS
  346.                                  RTFI_FilterFunc, &vol_filterhook,
  347. #endif
  348.                                  RTFI_VolumeRequest, 0, TAG_END))
  349.             rtEZRequest ("You picked the volume:\n'%s'",
  350.                              "Right", NULL, NULL, filereq->Dir);
  351.         else
  352.             rtEZRequest ("You didn't pick a volume.", "I did not", NULL, NULL);
  353.  
  354.         rtFreeRequest (filereq);
  355.         }
  356.     else
  357.         rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  358.  
  359.     rtEZRequestTags ("NUMBER 8: (ReqTools 2.0)\n"
  360.                           "Screen mode requester\n"
  361.                           "function: rtScreenModeRequest()\n"
  362.                           "Only available on Kickstart 2.0!",
  363.                           "_Proceed", NULL, NULL, RT_Underscore, '_', TAG_END);
  364.  
  365.     if (SysBase->LibNode.lib_Version < 37)
  366.         rtEZRequestTags ("Your Amiga doesn't seem to have\n"
  367.                               "Kickstart 2.0 in ROM so I am not\n"
  368.                               "able to show you the Screen mode\n"
  369.                               "requester.\n"
  370.                               "So upgrade to 2.0 *now* :-)",
  371.                               "_Allright", NULL, NULL, RT_Underscore, '_', TAG_END);
  372.     else {
  373.         if (scrmodereq = rtAllocRequestA (RT_SCREENMODEREQ, NULL)) {
  374.  
  375.             if (rtScreenModeRequest (scrmodereq, "Pick a screen mode:",
  376.                                              RTSC_Flags, SCREQF_DEPTHGAD|SCREQF_SIZEGADS|
  377.                                                  SCREQF_AUTOSCROLLGAD|SCREQF_OVERSCANGAD,
  378.                                               TAG_END))
  379.                 rtEZRequest ("You picked this mode:\n"
  380.                                  "ModeID  : 0x%lx\n"
  381.                                  "Size    : %ld x %ld\n"
  382.                                  "Depth   : %ld\n"
  383.                                  "Overscan: %ld\n"
  384.                                  "AutoScroll %s",
  385.                                  "Right", NULL, NULL,
  386.                                  scrmodereq->DisplayID,
  387.                                  scrmodereq->DisplayWidth,
  388.                                  scrmodereq->DisplayHeight,
  389.                                  scrmodereq->DisplayDepth,
  390.                                  scrmodereq->OverscanType,
  391.                                  scrmodereq->AutoScroll ? "On" : "Off");
  392.             else
  393.                 rtEZRequest ("You didn't pick a screen mode.", "Nope", NULL, NULL);
  394.  
  395.             rtFreeRequest (scrmodereq);
  396.             }
  397.         else
  398.             rtEZRequest ("Out of memory!", "Oh boy!", NULL, NULL);
  399.         }
  400.  
  401.     rtEZRequestTags ("That's it!\n"
  402.                           "Hope you enjoyed the demo", "_Sure did", NULL, NULL,
  403.                           RT_Underscore, '_', TAG_END);
  404.  
  405.     /* free all resources */
  406.     CloseLibrary ((struct Library *)ReqToolsBase);
  407.  
  408.     exit (0);
  409. }
  410.  
  411. /********
  412. * HOOKS *
  413. ********/
  414.  
  415. BOOL __asm __saveds file_filterfunc (
  416.     REG __a0 struct Hook *hook,
  417.     REG __a2 struct rtFileRequester *filereq,
  418.     REG __a1 struct FileInfoBlock *fib
  419.     )
  420. {
  421.     myputs (fib->fib_FileName);
  422.     myputs ("\n");
  423.     return (TRUE);
  424. }
  425.  
  426. BOOL __asm __saveds font_filterfunc (
  427.     REG __a0 struct Hook *hook,
  428.     REG __a2 struct rtFontRequester *fontreq,
  429.     REG __a1 struct TextAttr *textattr
  430.     )
  431. {
  432.     myputs (textattr->ta_Name);
  433.     myputs ("\n");
  434.     return (TRUE);
  435. }
  436.  
  437. BOOL __asm __saveds vol_filterfunc (
  438.     REG __a0 struct Hook *hook,
  439.     REG __a2 struct rtFileRequester *filereq,
  440.     REG __a1 struct rtVolumeEntry *volentry
  441.     )
  442. {
  443.     myputs (volentry->Type == DLT_DEVICE ? "(Volume) " : "(Assign) ");
  444.     myputs (volentry->Name);
  445.     myputs ("\n");
  446.     return (TRUE);
  447. }
  448.